import sys

import requests
from loguru import logger

from const import DIR_ENV
from lib.parse import read_file


class Base(object):
    def mer_send_request(self, api_name, data: dict):
        if self._MER_HOSR != '':
            data['url'] = self._MER_HOSR + data['url']
        logger.info(f'接口名称为{api_name}的请求数据为{data}')
        res = requests.request(**data).json()
        logger.info(f'接口名称为{api_name}的响应数据为{res}')
        return res

    def for_send_request(self, api_name, data: dict):
        if self._USER_HOST != '':
            data['url'] = self._USER_HOST + data['url']
        logger.info(f'接口名称为{api_name}的请求数据为{data}')
        res = requests.request(**data).json()
        logger.info(f'接口名称为{api_name}的响应数据为{res}')
        return res

    def pla_send_request(self, api_name, data: dict):
        if self._PLAT_HOSR != '':
            data['url'] = self._PLAT_HOSR + data['url']
        logger.info(f'接口名称为{api_name}的请求数据为{data}')
        res = requests.request(**data).json()
        logger.info(f'接口名称为{api_name}的响应数据为{res}')
        return res


class Login(Base):
    def __init__(self):
        # host
        PATH = DIR_ENV / 'test.yaml'
        res = read_file(str(PATH))
        self._MER_HOSR = res['merchant']['HOST']
        self._USER_HOST = res['user']['HOST']
        self._PLAT_HOSR = res['platform']['HOST']
        # token
        self._mer_token = self.__mer_login()['data']['token']
        self._for_token = self.__fro_login()['data']['token']
        self._pla_token = self.__pla_login()['data']['token']

    # 商家登录
    def __mer_login(self):
        data = {
            'url': '/api/admin/merchant/login',
            'method': 'post',
            'headers': {'Content-Type': 'application/json'},
            'json': {
                'account': '17300000000',
                'pwd': '000000',
                'captchaVO': {},
                'captcha': {
                    'captchaVerification': '',
                    'secretKey': '',
                    'token': '',
                },
            }
        }
        return self.mer_send_request(sys._getframe().f_code.co_name, data)

    # 用户登录
    def __fro_login(self):
        data = {
            'url': '/api/front/login/mobile/password',
            'method': 'post',
            'headers': {'Content-Type': 'application/json'},
            'json': {
                'phone': '13840191375',
                'password': 'wjw2002nian24',
                'spreadPid': 0,
            }
        }
        return self.for_send_request(sys._getframe().f_code.co_name, data)

    # 平台登录
    def __pla_login(self):
        data = {
            'url': '/api/admin/platform/login',
            'method': 'post',
            'headers': {'Content-Type': 'application/json'},
            'json': {
                'account': 'demo',
                'pwd': 'crmeb.com',
                'captchaVO': {},
            }
        }
        return self.pla_send_request(sys._getframe().f_code.co_name, data)


if __name__ == '__main__':
    Login()
